<?php

function sendemailAction($parameters, $node=NULL, $submission=NULL) {
  //send email
  //call workflow api return the email's template_name and address
  
  $default_email_template = 'HR1_thank_you_email';
  module_load_include('inc', 'webformview', 'webformview.sendemail');
  $data = array();
  $data['template_name'] = isset($parameters['email_template']) ? $parameters['email_template'] : $default_email_template;
  
  //------------send to receiver--------------
  //robert add 20130303
  $data['address'] = isset($parameters['receiver']) ? $parameters['receiver'] : '';
  //if(empty($data['address'])){return;}
  $data['address'] = explode(",", $data['address']);
  //------------end send to receiver--------------
  
  //------------send to roles--------------
  $role = isset($parameters['role']) ? $parameters['role'] : '';
  $all_roles = user_roles();
  $key = array_search($role, $all_roles);
  if($key){
    $query = db_select('users', 'u',array('fetch' => PDO::FETCH_ASSOC));
    $query->leftJoin('users_roles', 'r', 'u.uid = r.uid');
    $query->fields('u', array('mail'));
    $query->condition('r.rid', $key);
    $result = $query->execute();
    foreach ($result as $rs) {
      if($rs['mail']){
        $data['address'][] = $rs['mail'];
      }
    }
  }
  //------------end send to roles------------
  
  //if_send_initiator
  $data['if_send_initiator'] = isset($parameters['if_send_initiator']) ? $parameters['if_send_initiator'] : '0';
  
  //error_log('sendemailAction:'.print_r($parameters,1));
  webformview_send_email($data, $node, $submission);
  //end send email
}






function synchronizeToInteract($parameters, $node=NULL, $submission=NULL) {
  global $base_url;
  if (module_exists('relo_sendxml')) {
    //error_log('relo_sendxml:'.print_r($parameters,1));
    $results = module_invoke('relo_sendxml', 'submission_interact', $node, $submission); //print_r($results->statusCode);die;
    if ($results['statusCode'] == '200') {
      drupal_set_message($results['statusMessage']);
    } else {
      drupal_set_message($results['statusMessage'],'error');
      //-------------------------------send email to IT_ADMIN-------------------------------
      module_load_include('inc', 'webformview', 'webformview.sendemail');
      $email = array();
      $role = ADMINIT_ROLE;
      $all_roles = user_roles();
      $key = array_search($role, $all_roles);
      if($key){
        $query = db_select('users', 'u',array('fetch' => PDO::FETCH_ASSOC));
        $query->leftJoin('users_roles', 'r', 'u.uid = r.uid');
        $query->fields('u', array('mail'));
        $query->condition('r.rid', $key);
        $result = $query->execute();
        foreach ($result as $rs) {
          if($rs['mail']){
            $email['address'][] = $rs['mail'];
          }
        }
      }
      
      $email['addresscc'] = array();
      $email['subject'] = 'Synchronize To Interact Failure';
      $email['body'] = 'The submission synchronize to Interact failure';
      $email['body'] .=  "\n"."statusCode:".$results['statusCode'].', statusMessage:'.$results['statusMessage'];
      $link_url = $base_url.'/node/'.$node->nid.'/submission/'.$submission->sid;
      $link = t('Please click this below link for more details:').$link_url ;
      $email['body'] .=  "\n".$link;
      
      postmail_webformview($email['address'], $email['addresscc'], $email['subject'], $email['body']);
      //-------------------------------end send email to IT_ADMIN-------------------------------
    }
  }
}






